package com.example.wm.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wm.common.Result;
import com.example.wm.common.ResultData;
import com.example.wm.entity.Custonmer;
import com.example.wm.mapper.CustonmerMapper;

import com.example.wm.service.CustonmerService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;


@Slf4j
@RestController
@RequestMapping("/ClientController")
@CrossOrigin
//客户
public class ClientController {
    @Resource
    private CustonmerService custonmerService;
    @Resource
    private CustonmerMapper custonmerMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Transactional
    @PostMapping("/getlist")
    public Result getlist() {
        Set<String> s= redisTemplate.keys("customer*");
//        redisTemplate.delete(s);
        ArrayList<Object> alist=new ArrayList<>();
        if(!s.isEmpty()){
          for (String set:s){
             Object obs=   redisTemplate.opsForValue().get(set);
             alist.add(obs);
          }
          return ResultData.success(alist);

        }else {
            List<Custonmer> list=custonmerService.list();
            for(Custonmer c:list){
            redisTemplate.opsForValue().set("customer"+c.getId(),c);
            }
            return ResultData.success(list);

        }

    }
//    重新加载数据
    @PostMapping("/regetlist")
    public Result regetlist() {
        Set<String> s= redisTemplate.keys("customer*");
      Long b=  redisTemplate.delete(s);

            return ResultData.success(b);


    }
//    添加备注
@Transactional
@PostMapping("/addtexts")
public Result addtexts(@RequestBody Custonmer custonmer) {
        LambdaQueryWrapper<Custonmer> lq=new LambdaQueryWrapper<>();
         lq.eq(Custonmer::getId,custonmer.getId());
         Custonmer c=custonmerService.getOne(lq);
    if(c.getDetails()==null){

        custonmer.setDetails(custonmer.getDetails());
    }else {
        custonmer.setDetails(c.getDetails() + ";" + custonmer.getDetails());
    }

    Set s=redisTemplate.keys("customer*");
    redisTemplate.delete(s);
        Boolean b=  custonmerService.updateById(custonmer);

    return ResultData.success(b);

}
//    修改评级
@Transactional
@PostMapping("/updatelever")
public Result updatelever(@RequestBody Custonmer custonmer) {
    LambdaQueryWrapper<Custonmer> lq=new LambdaQueryWrapper<>();
    lq.eq(Custonmer::getId,custonmer.getId());
    Custonmer c=custonmerService.getOne(lq);
    if(c.getDetails()==null){

        custonmer.setDetails(custonmer.getDetails());
    }else {
        custonmer.setDetails(c.getDetails()+";"+custonmer.getDetails());
    }



    Set s=redisTemplate.keys("customer*");
    redisTemplate.delete(s);

    Boolean b=  custonmerService.updateById(custonmer);
    return ResultData.success(b);

}
//添加点
@Transactional
@PostMapping("/save")
public Result savekh(@RequestBody Custonmer custonmer) {

    Boolean b=  custonmerService.save(custonmer);
    System.out.println(custonmer.getId()+":iddddd");
//    Set<String> s= redisTemplate.keys("customer*");
//    Long bs=  redisTemplate.delete(s);
//    redisTemplate.opsForValue().set("customer"+custonmer.getId(),custonmer);
//    Set ss=redisTemplate.keys("customer*");
//    redisTemplate.delete(ss);
    return ResultData.success(b);

}

    @PostMapping("/page")
    public Result page(@RequestBody JSONObject jsonObject) {
        Page<Custonmer> pageInfo = new Page<>(jsonObject.getLong("page"),jsonObject.getLong("pageSize"));
        LambdaQueryWrapper<Custonmer> lq=new LambdaQueryWrapper<>();
        Page<Custonmer>  list=custonmerService.page(pageInfo,lq);
        System.out.println(pageInfo);

        return ResultData.success(pageInfo);
    }
    @PostMapping("/delete")
    public Result delete(@RequestBody Custonmer custonmer) {

        LambdaQueryWrapper<Custonmer> lq=new LambdaQueryWrapper<>();
        lq.eq(Custonmer::getId,custonmer.getId());
         Boolean b= custonmerService.removeById(custonmer);
        return ResultData.success(b);
    }
    @PostMapping("/update")
    public Result update(@RequestBody Custonmer custonmer) {

        LambdaQueryWrapper<Custonmer> lq=new LambdaQueryWrapper<>();
        lq.eq(Custonmer::getId,custonmer.getId());
        Boolean b= custonmerService.updateById(custonmer);
        return ResultData.success(b);
    }
    @PostMapping("/findbyname")
    public Result findbyname(@RequestBody Custonmer custonmer) {

        LambdaQueryWrapper<Custonmer> lq=new LambdaQueryWrapper<>();
        lq.like(Custonmer::getCustomername,custonmer.getCustomername());
        List<Custonmer> list= custonmerService.list(lq);
        return ResultData.success(list);
    }
}
